using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Edge.Common.ComponentModel.Notification;
using Edge.Common.Services;
using Edge.Common.ComponentModel.Notification;
using Edge.Common.Services;

namespace Edge.Common.Web.Notification
{
    [Serializable]
    public class NotifyHolder : Label, INotificationListener
    {
        #region Render strategies

        interface IRenderStrategy
        {
            void Render(HtmlTextWriter writer);
        }

        [Serializable]
        class EmptyRenderStrategy : IRenderStrategy
        {
            public void Render(HtmlTextWriter writer)
            {
                // do nothing
            }
        }

        [Serializable]
        class ErrorRenderStrategy : IRenderStrategy
        {
            private string text;


            public ErrorRenderStrategy(string text)
            {
                this.text = text;
            }

            public void Render(HtmlTextWriter writer)
            {
                const string markupTemplate = @"<div style=""border: solid 1px #cc0000;background-color: transparent;margin-top: 10px;margin-bottom: 10px;padding-left: 10px;padding-top: 10px; width:50%;"">
    <b>Error</b>
    <P>
    {0}
    </P>
</div>";
                writer.Write(string.Format(markupTemplate, HttpUtility.HtmlEncode(text)));
            }
        }

        [Serializable]
        class NotifyRenderStrategy : IRenderStrategy
        {
            private string text;


            public NotifyRenderStrategy(string text)
            {
                this.text = text;
            }

            public void Render(HtmlTextWriter writer)
            {
                const string markupTemplate = @"<div style=""border: solid 1px #3c78b5;background-color: transparent;margin-top: 10px;margin-bottom: 10px;padding-left: 10px;padding-top: 10px; width:50%;"">
    <b>Notify</b>
    <P>
    {0}
    </P>
</div>";

                writer.Write(string.Format(markupTemplate, HttpUtility.HtmlEncode(text)));
            }
        }

        #endregion

        private const string SESSION_KEY = "NotifyHolderRenderStrategy";

        private List<IRenderStrategy> notifiers = new List<IRenderStrategy>();

        ///<summary>
        ///Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        ///</summary>
        ///
        ///<param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data. </param>
        protected override void OnInit(EventArgs e)
        {
            Locator.GetService<INotificationManager>().RegisterListener(this);
            base.OnInit(e);
        }


        public void Notify(string msg)
        {
            notifiers.Add(new NotifyRenderStrategy(msg));
            HttpContext.Current.Session[SESSION_KEY] = notifiers;
        }

        public void Error(string msg)
        {
            notifiers.Add(new ErrorRenderStrategy(msg));
            HttpContext.Current.Session[SESSION_KEY] = notifiers;
        }


        protected override void Render(HtmlTextWriter writer)
        {
            foreach (IRenderStrategy renderStrategy in notifiers)
            {
                renderStrategy.Render(writer);
            }
            HttpContext.Current.Session[SESSION_KEY] = null;
        }

        public override bool EnableViewState
        {
            get { return false; }
        }


        protected override void OnPreRender(EventArgs e)
        {
            if (HttpContext.Current.Request.HttpMethod.ToLower() == "get" && HttpContext.Current.Session[SESSION_KEY] != null)
            {
                notifiers = (List<IRenderStrategy>)HttpContext.Current.Session[SESSION_KEY];
            }
            base.OnPreRender(e);
        }
    }
}